package com.hmall.search.listeners;

import cn.hutool.json.JSONUtil;
import com.hmall.api.client.ItemClient;
import com.hmall.api.dto.ItemDTO;
import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.common.constants.EsConstants;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.domain.R;
import com.hmall.common.utils.BeanUtils;
import com.hmall.search.domain.po.ItemDoc;
import com.rabbitmq.client.AMQP;
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

import static org.springframework.amqp.rabbit.config.ListenerContainerFactoryBean.Type.direct;

@Component
@RequiredArgsConstructor
@Slf4j
public class ItemListener {

    private final ItemClient itemClient;

    private final RestHighLevelClient client=new RestHighLevelClient(
            RestClient.builder(HttpHost.create("192.168.177.128:9200"))
    ); //es初始化
    /**
     * 监听新增商品的消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.ITEM_SEARCH_SAVE_QUEUE_NAME,durable = "true"),
            exchange = @Exchange(name = MqConstants.ITEM_SEARCH_EXCHANGE_NAME),
            key = MqConstants.ITEM_SEARCH_SAVE_KEY
    ))
    public void saveListener(Long itemId) throws IOException {
        log.info("开始监听新增商品的队列....");

        //1.调用商品服务，根据id查询商品
        ItemDTO itemDTO = itemClient.queryItemById(itemId);
        //将ItemDTO转为ItemDOC的类型
        ItemDoc itemDoc = BeanUtils.copyBean(itemDTO, ItemDoc.class);
        //2.使用es的api将数据存储到items的索引库中
        //2.1 创建新增文档请求
        IndexRequest request=new IndexRequest(EsConstants.INDEX_NAME).id(itemDoc.getId());
        //2.2 准备参数
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        request.source(jsonStr, XContentType.JSON);
        //2.3 发送请求
        client.index(request, RequestOptions.DEFAULT);

    }

    /**
     * 监听删除商品的消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.ITEM_SEARCH_DELETE_QUEUE_NAME,durable = "true"),
            exchange = @Exchange(name = MqConstants.ITEM_SEARCH_EXCHANGE_NAME),
            key = MqConstants.ITEM_SEARCH_DELETE_KEY
    ))
    public void deleteListener(Long id) throws IOException {
        log.info("开始监听删除商品的队列....");
        System.out.println("需要删除商品的id为： " + id);

        //1.创建删除文档的请求
        DeleteRequest request=new DeleteRequest(EsConstants.INDEX_NAME).id(id.toString());

        //2.发送请求
        client.delete(request, RequestOptions.DEFAULT);
    }

    /**
     * 监听更新商品的消息
     * 使用全局修改
     * 即当前id存在，则更新当前id文档
     * 当前id不存在，新增文档
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.ITEM_SEARCH_UPDATE_QUEUE_NAME,durable = "true"),
            exchange = @Exchange(name = MqConstants.ITEM_SEARCH_EXCHANGE_NAME),
            key = MqConstants.ITEM_SEARCH_UPDATE_KEY
    ))
    public void updateListener(ItemDTO itemDTO) throws IOException{
        log.info("开始监听更新商品的队列....");
        //1.将DTO转为DOC类型
        ItemDoc itemDoc = BeanUtils.copyBean(itemDTO, ItemDoc.class);
        //2.创建文档请求
        IndexRequest request=new IndexRequest(EsConstants.INDEX_NAME).id(itemDoc.getId());
        //3.准备参数
        String jsonStr = JSONUtil.toJsonStr(itemDoc);
        request.source(jsonStr,XContentType.JSON);
        //4.发送请求
        client.index(request,RequestOptions.DEFAULT);

    }
}
